home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / UserCode / compare-windows.tcl < prev    next >
Text File  |  1994-03-08  |  5KB  |  237 lines

  1. # FILE: compare-windows.tcl
  2. #
  3. # LAST UPDATE: 01/06/93 5:24:32 AM
  4. #
  5. # This file contains the following TCL procedure(s):
  6. #
  7. #    compare-windows -- Compare two windows from the cursor and position
  8. #                       the cursors at the next difference
  9. #
  10. # TCL SYNTAX:
  11. #
  12. #    NOTE: The options are not yet fully functional
  13. #
  14. #    compare-windows [-w|-b] [-c] [file1] [file2]
  15. #
  16. #    -b ignore blanks and tabs
  17. #    -w ignore blacks, tabs, newlines, returns
  18. #    -c case insenstive (ie. A matches a)
  19. #
  20. #    To use, simply source this file place it in the a folder with the
  21. #    name $HOME:Tcl:Usercode: and invoke it implicitly via the "unknown proc".
  22. #
  23. # SEE ALSO unknown.tcl
  24.  
  25. # COPYRIGHT:
  26. #
  27. #    Copyright ⌐ 1992,1993 by David C. Black
  28. #    All rights reserved.
  29. #
  30. #    Redistribution and use in source and binary forms are permitted
  31. #    provided that the above copyright notice and this paragraph are
  32. #    duplicated in all such forms and that any documentation,
  33. #    advertising materials, and other materials related to such
  34. #    distribution and use acknowledge that the software was developed
  35. #    by David C. Black.
  36. #
  37. #    THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  38. #    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39. #    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40. #
  41. ################################################################################
  42.  
  43. # AUTHOR
  44. #
  45. #    David C. Black
  46. #    Internet: black@mpd.tandem.com (preferred)
  47. #    GEnie:    D.C.Black
  48. #    USnail:   6217 John Chisum Lane, Austin, TX 78749
  49. #
  50. ################################################################################
  51.  
  52. # HISTORY
  53. #                  
  54. # modified who rev reason
  55. # -------- --- --- ------
  56. # 01/06/93 DCB 1.0 Original
  57.  
  58. proc compare-windows {args} {
  59.  
  60.     set patt {}
  61.     set fold 0
  62.     set wins [winNames]
  63. #    set wins [winNames -f]
  64.     set files {}
  65.     if {[llength $args] > 0} {
  66.         foreach arg $args {
  67.             case $arg {
  68.                 {-c} {
  69.                     set fold 1
  70.                 }
  71.                 {-b} {
  72.                     set patt "\[ \t\]+"
  73.                 }
  74.                 {-w} {
  75.                     set patt "\[ \t\n\r\]+"
  76.                 }
  77.                 {} {
  78.                 }
  79.                 default {
  80.                     if {[string first ":" $arg] < 0} {
  81.                         set arg "[pwd]$arg"
  82.                     }
  83.                     set found 0
  84.                     foreach win $wins {
  85.                         if {$arg == $win} {
  86.                             set found 1
  87.                             break
  88.                         }
  89.                     }
  90.                     if {$found} {
  91.                         lappend files $arg
  92.                     } else {
  93.                         if {[file exists $arg]} {
  94.                             lappend files $arg
  95.                             edit $arg -r
  96.                         } else {
  97.                             alertnote "Cannot read $arg"
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     set files [concat $files $wins]
  105.  
  106.     if {[llength $files] < 2} {
  107.         alertnote "Need at least 2 windows/files"
  108.         return
  109.     }
  110.  
  111.     # Get window 1 text to compare
  112.     set wn(1) [lindex $files 0]
  113.     bringToFront $wn(1)
  114.     if {[getSelect] != ""} {
  115.         forwardChar
  116.     }
  117.     set wp(1) [getPos]
  118.     set wo(1) [getText $wp(1) [maxPos]]
  119.     set wt(1) $wo(1)
  120.     if {$patt != ""} {
  121.         regsub -all $patt $wt(1) " " wt(1)
  122.     }
  123.     if {$fold} {
  124.         set wt(1) [string tolower $wt(1)]
  125.     }
  126.  
  127.     # Get window 2 text to compare
  128.     set wn(2) [lindex $files 1]
  129.     bringToFront $wn(2)
  130.     if {[getSelect] != ""} {
  131.         forwardChar
  132.     }
  133.     set wp(2) [getPos]
  134.     set wo(2) [getText $wp(2) [maxPos]]
  135.     set wt(2) $wo(2)
  136.     if {$patt != ""} {
  137.         regsub -all $patt $wt(2) " " wt(2)
  138.     }
  139.  
  140.     # Exactly equal
  141.     if {$wt(1) == $wt(2)} {
  142.         message "Identical"
  143.         return
  144.     }
  145.  
  146.     # Only consider smaller of two strings    
  147.     set siz [string length $wt(1)]
  148.     if {$siz > [string length $wt(2)]} {
  149.         set siz [string length $wt(2)]
  150.     }
  151.     
  152.     # Equal except for added stuff
  153.     set l [expr {$siz-1}]
  154.     if {[string range $wt(1) 0 $l] == [string range $wt(2) 0 $l]} {
  155.         set beg $siz
  156.         set offset(1) $beg
  157.         set offset(2) $beg
  158.     } else {
  159.         set beg 0
  160.         set trace {}
  161.     
  162.         while {$siz} {
  163.             set siz [expr {$siz / 2}]
  164.             set end [expr {$beg+$siz}]
  165.             if {[string range $wt(1) $beg $end] == [string range $wt(2) $beg $end]} {
  166.                 incr beg $siz
  167.                 incr beg
  168.             }
  169.         }
  170.  
  171.         set offset(1) $beg
  172.         set offset(2) $beg
  173.     
  174.         # adjust for $patt
  175.         if {$patt != ""} {
  176.             foreach i {1 2} {
  177.                 set front ""
  178.                 set left [expr {$beg+1}]
  179.                 set back $wo($i)
  180.                 set iterations 0
  181.                 while {$left > 0} {
  182.                     append front [string range $back 0 [expr {$left-1}]]
  183.                     set    back  [string range $back $left end]
  184.                     regsub -all $patt $front { } front
  185.                     set left [expr {$beg + 1 - [string length $front]}]
  186.                     incr offset($i) $left
  187.                     if {[incr iterations] > 100} {
  188.                         alertnote "diff: iterations exceeded 100"
  189.                         break
  190.                     }
  191.         #BEG DEBUG
  192.         global testing
  193.         if {$testing} {
  194.             set o $offset($i)
  195.             regsub -all "\[\r\n\]*" $front "Ñ" t
  196.             if {[getline "$i╟$t╚ $left/$o" "continue"] != "continue"} {
  197.                 break
  198.             }
  199.         }
  200.         #END DEBUG
  201.                 }
  202.             }
  203.             
  204.         }
  205.     }
  206.     
  207.  
  208. #    bringToFront $wn(2)
  209.     set pos [expr {$wp(2)+$offset(2)}]
  210.     if {$pos > [maxPos]} {
  211.         goto end
  212.     } else {
  213.         goto $pos
  214.     }
  215.     centerRedraw
  216.     forwardCharSelect
  217.  
  218.     bringToFront $wn(1)
  219. #    listpick $trace
  220.     set pos [expr {$wp(1)+$offset(1)}]
  221.     if {$pos > [maxPos]} {
  222.         goto end
  223.     } else {
  224.         goto $pos
  225.     }
  226.     centerRedraw
  227.     forwardCharSelect
  228.  
  229.     return
  230. }
  231.  
  232. set testing 0
  233. if {$testing} {
  234.     set cwd [pwd]
  235.     diff -b "${cwd}TCLs:1.txt" "${cwd}TCLs:2.txt"
  236. }
  237.